---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-metrics-adapter-config
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list .) | nindent 2 }}
data:
  config.yaml: |
    externalRules:
    - seriesQuery: '{__name__=~"kube_adapter_metric_.+"}'
      resources:
        namespaced: false
      name:
        matches: "^kube_adapter_metric_(.+)$"
        as: "${1}"
      metricsQuery: 'label_replace(<<.Series>>{<<.LabelMatchers>>}, "__name__", "$1", "__name__", "^kube_adapter_metric_(.*)$")'
    resourceRules:
      cpu:
        containerQuery: sum(irate(container_cpu_usage_seconds_total{<<.LabelMatchers>>, container!="POD"}[{{ mul (.Values.global.discovery.prometheusScrapeInterval | default 30) 4 }}s])) by (<<.GroupBy>>)
        nodeQuery: sum(irate(container_cpu_usage_seconds_total{<<.LabelMatchers>>}[{{ mul (.Values.global.discovery.prometheusScrapeInterval | default 30) 4 }}s])) by (<<.GroupBy>>)
        resources:
          overrides:
            node:
              resource: node
            namespace:
              resource: namespace
            pod:
              resource: pod
        containerLabel: container
      memory:
        containerQuery: sum(container_memory_working_set_bytes:without_kmem{<<.LabelMatchers>>, container!="POD"}) by (<<.GroupBy>>)
        nodeQuery: sum(container_memory_working_set_bytes:without_kmem{<<.LabelMatchers>>}) by (<<.GroupBy>>)
        resources:
          overrides:
            node:
              resource: node
            namespace:
              resource: namespace
            pod:
              resource: pod
        containerLabel: container
      window: 0s
    rules:
    - seriesQuery: 'ingress_nginx_detail_requests_total{namespace!="",ingress!=""}'
      resources:
        overrides:
          namespace: {resource: namespace}
          ingress: {resource: ingress}
      name:
        matches: ".*"
        as: "rps_1m"
      metricsQuery: 'sum(irate(<<.Series>>{<<.LabelMatchers>>}[5m])) by (<<.GroupBy>>) OR on() vector(0)'
    - seriesQuery: 'container_cpu_usage_seconds_total{namespace!="",pod!="",container!="POD"}'
      resources:
        overrides:
          namespace: {resource: namespace}
          pod: {resource: pod}
      name:
        matches: ".*"
        as: "cpu_1m"
      metricsQuery: 'sum(irate(<<.Series>>{<<.LabelMatchers>>, container!="POD"}[5m])) by (<<.GroupBy>>)'
    - seriesQuery: 'container_memory_working_set_bytes:without_kmem{namespace!="",pod!="",container!="POD"}'
      resources:
        overrides:
          namespace: {resource: namespace}
          pod: {resource: pod}
      name:
        matches: ".*"
        as: "memory_1m"
      metricsQuery: 'sum(avg_over_time(<<.Series>>{<<.LabelMatchers>>, container!="POD"}[1m])) by (<<.GroupBy>>)'
{{- range list "5m" "15m" }}
    - seriesQuery: 'ingress_nginx_detail_requests_total{namespace!="",ingress!=""}'
      resources:
        overrides:
          namespace: {resource: namespace}
          ingress: {resource: ingress}
      name:
        matches: ".*"
        as: "rps_{{ . }}"
      metricsQuery: 'sum(rate(<<.Series>>{<<.LabelMatchers>>}[{{ . }}])) by (<<.GroupBy>>) OR on() vector(0)'
    - seriesQuery: 'container_cpu_usage_seconds_total{namespace!="",pod!="",container!="POD"}'
      resources:
        overrides:
          namespace: {resource: namespace}
          pod: {resource: pod}
      name:
        matches: ".*"
        as: "cpu_{{ . }}"
      metricsQuery: 'sum(rate(<<.Series>>{<<.LabelMatchers>>, container!="POD"}[{{ . }}])) by (<<.GroupBy>>)'
    - seriesQuery: 'container_memory_working_set_bytes:without_kmem{namespace!="",pod!="",container!="POD"}'
      resources:
        overrides:
          namespace: {resource: namespace}
          pod: {resource: pod}
      name:
        matches: ".*"
        as: "memory_{{ . }}"
      metricsQuery: 'sum(avg_over_time(<<.Series>>{<<.LabelMatchers>>, container!="POD"}[{{ . }}])) by (<<.GroupBy>>)'
{{- end }}
{{- range (list "pod" "deployment" "daemonset" "statefulset" "ingress" "service" "namespace") }}
  {{- $resource := . }}
  {{- if hasKey $.Values.prometheusMetricsAdapter.internal.customMetrics $resource }}
    {{- range  (pluck $resource $.Values.prometheusMetricsAdapter.internal.customMetrics | first | keys | sortAlpha) }}
    {{- $metric_name := . }}
    - seriesQuery: 'kube_{{ $resource }}_created'
      resources:
        overrides:
          namespace: {resource: namespace}
{{- if not (eq $resource "namespace") }}
          {{ $resource }}: {resource: {{ $resource }}}
{{- end }}
      name:
        matches: ".*"
        as: "{{ $metric_name }}"
      metricsQuery: 'custom_metric::{{ $resource }}::{{ $metric_name }}::<<.LabelMatchers>>::<<.GroupBy>>'
    {{- end }}
  {{- end }}
{{- end }}
  reverse-proxy.json: |
    {{- .Values.prometheusMetricsAdapter.internal.customMetrics | toJson | nindent 4}}
